// pages/sponsor/sponsor.js
var api = require("../../utils/api.js");
var util = require("../../utils/util.js");
var click = require("../template/commodity/commodity.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSuccess: false,
    exchangeSuccess: false,
    commoditys: [],
    pageIndex: 1,
    pageNum: 1,
    userInfo: {},
    sponsorStep: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // type = 0  赞助   其他  兑换成功
    var info = options.userInfo ? JSON.parse(options.userInfo) : {};
    var type = options.type ? options.type : 0;
    // info.openid = "owKbq0NWGk8QAaRM95FCw9H0WogA";
    wx.setNavigationBarTitle({
      title: type!= 0 ? '达人步兑' : '赞助',
    })
    if (type != 0) {
      this.setData({
        exchangeSuccess: type != 0,
        isSuccess: type != 0,
      })
    } else {
      api.helpRecordData(info.openid, res => {
        this.setData({
          userInfo: res,
        })
      })
    }

    this.getCommoditys(false);
  },

  //获取商品
  getCommoditys: function(load) {
    if (!load) {
      this.setData({
        pageIndex: 1,
      });
    }
    api.goods(0, this.data.pageIndex, 1, (goods, allIndex) => {
      this.setData({
        pageIndex: ++this.data.pageIndex,
        pageNum: allIndex,
        commoditys: load ? this.data.commoditys.concat(goods) : goods,
      });
    });
  },

  //点击商品
  clickCommodity: function(e) {
    click.commodity(e.currentTarget.dataset.commodity, e.currentTarget.dataset.type);
  },

  inputChange: function(e) {
    var stepNum = parseInt(e.detail.value ? e.detail.value : 0);
    var maxNum = Math.min(parseInt(this.data.userInfo.this_user.step), parseInt(this.data.userInfo.max_step));
    // console.log(stepNum + "   " + maxNum);
    var returnStr;
    if (stepNum > maxNum) {
      returnStr = maxNum;
    } else {
      returnStr = stepNum;
    }
    if (returnStr <= 0) {
      returnStr = '';
    }
    this.setData({
      sponsorStep: returnStr,
    })
    return returnStr;
  },

  //赠送步数成功
  sponsor: function(e) {
    // this.data.sponsorStep = 0;
    wx.showLoading({})
    api.sendStep(this.data.userInfo.send_user.user_id, this.data.sponsorStep, res => {
      this.setData({
        isSuccess: true,
      })
      wx.hideLoading();
    }, res => {
      wx.hideLoading();
      wx.showToast({
        title: res,
        icon: 'none'
      })
    });
  },
  //兑换成功
  exchange: function(e) {
    wx.navigateBack({

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getCommoditys(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getCommoditys(true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return app.shareSeekHelp();
  }
})